package com.gxa.component.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 发布者：生产者，生产消息。
 */
public class PublisherMain {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1.建立连接
        ConnectionFactory connectionFactory = new ConnectionFactory();
        // rabbitmq主机地址 = ip
        connectionFactory.setHost("121.4.166.75");
        // 通信端口5672 - 记得关闭防火墙
        connectionFactory.setPort(5672);
        // 虚拟主机地址
        connectionFactory.setVirtualHost("/");
        // 用户名
        connectionFactory.setUsername("gxa");
        // 密码
        connectionFactory.setPassword("123456");
        // 获取连接
        Connection connection = connectionFactory.newConnection();
        // 2.创建channel
        Channel channel = connection.createChannel();
        // 3.声明队列
        final  String QUEUE_NAME = "simple.queue";
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        // 4.发送消息
        String message = "Hello RabbitMQ";
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println("发送消息成功："+message);
        // 5.关闭连接和channel
        channel.close();
        connection.close();
    }

}
